package com.utility.project.common.generate;

import cn.hutool.core.util.ClassUtil;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.util.Collections;

/**
 * @author wangjiyun
 * @date 2022/12/27 17:08
 * @description 代码生成主类
 */
public class GenRun {

    public static void main(String[] args) {

        String classPath = ClassUtil.getClassPath();
        String s = classPath.replaceAll("target/classes/", "");
        String rootPath = s + "src/main/java";
        String rootResPath = s + "src/main/resources/mapper";

        FastAutoGenerator.create("jdbc:mysql://39.103.156.125:3306/yasi", "yasi", "yasi2023")
            .globalConfig(builder -> {
                builder.author("yangzhiguo") // 设置作者
                    .outputDir(rootPath); // 指定输出目录
            }).packageConfig(builder -> {
                builder.parent("com.utility.project") // 设置父包名
                    .pathInfo(Collections.singletonMap(OutputFile.xml,rootResPath)); // 设置mapperXml生成路径
            }).strategyConfig(builder -> {
                builder.addInclude("ys_exam_answer") // 设置需要生成的表名
                .addTablePrefix(""); // 设置过滤表前缀
                builder.controllerBuilder().enableRestStyle();
                builder.entityBuilder().enableLombok();
                builder.mapperBuilder().enableBaseResultMap();
                builder.mapperBuilder().enableBaseColumnList();
            }).templateConfig(builder -> {
                builder.entity("/templates/entity.java").service("/templates/service.java")
                    .serviceImpl("/templates/serviceImpl.java").mapper("/templates/mapper.java")
                    .controller("/templates/controller.java").build();
            }).execute();
    }
}
